<script setup lang="ts">
import type {
  KnowledgeList,
  KnowledgeParams,
  KnowledgeType
} from '@/types/consult'
import { ref } from 'vue'
import KnowledgeCard from './KnowledgeCard.vue'
import { getKnowledgePage } from '@/services/consult'

// 0. 定义属性接收外部传入数据
const props = defineProps<{
  type: KnowledgeType
}>()

// 1. 加载列表数据
const list = ref<KnowledgeList>([])
const loading = ref(false) // 控制列表的加载状态
const finished = ref(false) // 控制列表是否加载完数据
// 查询参数
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1, // 当前页
  pageSize: 10 // 每页请求条数
})
const onLoad = async () => {
  // 1. 调用接口
  const res = await getKnowledgePage(params.value)
  // console.log(res)
  // 注意: 要往数组尾部追加数据
  list.value.push(...res.data.rows)
  // 2. 判断是否加载完数据
  if (params.value.current >= res.data.pageTotal) {
    // 数据加载完毕
    finished.value = true
  } else {
    // 当前页面++
    params.value.current++
  }
  // 3. 改变loading的状态
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <KnowledgeCard
        v-for="item in list"
        :key="item.id"
        :item="item"
      ></KnowledgeCard>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
